import InstrumentationPackage.*;
import EventPackage.*;

class SecurityMonitor extends EventProcessor implements Runnable {

    private static final long DELAY_BETWEEN_EVENTS_MILLIS = 500L;
    private MessageWindow messageWindow; // This is the message window

    public SecurityMonitor() {
        this(null);
    }

    public SecurityMonitor(String eventManagerIpAddress) {
        super(eventManagerIpAddress, DELAY_BETWEEN_EVENTS_MILLIS);
    }

    @Override
    protected EventType[] getEventTypesToRegister() {
        return new EventType[]{
            EventType.CONSOLE_MESSAGE,
            EventType.MOTION_SECURITY_EVENT,
            EventType.DOOR_SECURITY_EVENT,
            EventType.WINDOW_SECURITY_EVENT
        };
    }   

    private static final MessageWindow createMessageWindow(String windowName, float posX, float posY) {
        return new MessageWindow(windowName, posX, posY);
    }

    @Override
    public void run() {
        try {
            processEvents();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onRegistered() {
        messageWindow = createMessageWindow("Security Monitor Console", 0.05f, 0.5f);
        messageWindow.WriteMessage("Registered security monitor");
    }

    @Override
    protected void onUnregistered() {
        messageWindow.WriteMessage("Unregistered security monitor");
    }

    @Override
    protected void onProcessing() {

    }

    @Override
    protected void onEvent(Event event) {
        if (EventType.CONSOLE_MESSAGE.isEvent(event.GetEventId(), event.GetMessage())) {
            writeMessage(event.GetMessage());
        } else if (EventType.MOTION_SECURITY_EVENT.isEvent(event.GetEventId(), event.GetMessage())) {
            tripMotionAlarm();
        } else if (EventType.DOOR_SECURITY_EVENT.isEvent(event.GetEventId(), event.GetMessage())) {
            tripDoorAlarm();
        } else if (EventType.WINDOW_SECURITY_EVENT.isEvent(event.GetEventId(), event.GetMessage())) {
            tripWindowAlarm();
        }
    }

    private void writeMessage(String message) {
        if (messageWindow != null) {
            messageWindow.WriteMessage(message);
        }
    }

    public void halt() {
        writeMessage("*** COMMAND:  Shutdown System ***");
        sendEvent(new Event(EventType.TERMINATE.getEventId(), "shutdown system"));
    }

    public void armSecuritySystem() {
        writeMessage("*** COMMAND:  Arm Security System ***");
        sendEvent(new Event(EventType.ARMED_EVENT_ON.getEventId(), EventType.ARMED_EVENT_ON.getEventMessage()));
    }

    public void disarmSecuritySystem() {
        writeMessage("*** COMMAND:  Disarm Security System ***");
        sendEvent(new Event(EventType.ARMED_EVENT_OFF.getEventId(), EventType.ARMED_EVENT_OFF.getEventMessage()));
    }

    public void tripMotionAlarm() {
        writeMessage("*** COMMAND:  Trip Motion Alarm ***");
        sendEvent(new Event(EventType.MOTION_ALARM_ON.getEventId(), EventType.MOTION_ALARM_ON.getEventMessage()));
    }

    public void tripDoorAlarm() {
        writeMessage("*** COMMAND:  Trip Door Alarm ***");
        sendEvent(new Event(EventType.DOOR_ALARM_ON.getEventId(), EventType.DOOR_ALARM_ON.getEventMessage()));
    }

    public void tripWindowAlarm() {
        writeMessage("*** COMMAND:  Trip Window Alarm ***");
        sendEvent(new Event(EventType.WINDOW_ALARM_ON.getEventId(), EventType.WINDOW_ALARM_ON.getEventMessage()));
    }

}
